package cn.format;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import cn.format.b.JsonFormatB;

public class MainUI extends JFrame {
    private static final long serialVersionUID = -8506892428672551866L;
    private static JTextArea mSrcText = new JTextArea();
    private static JTextArea mDestText = new JTextArea();
    private JPanel contentPane;
    private static String mCopyText = "";

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainUI frame = new MainUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainUI() {
        setTitle("Json格式化工具1.0(左键双击拷贝结果,右键双击还原系统粘贴板)");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(1, 2));
        getContentPane().add(contentPane);

        // 获取屏幕大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(screenSize.width / 3 * 2, screenSize.height / 3 * 2);
        // 让窗口居中显示
        setLocation(screenSize.width / 2 - screenSize.width / 3, screenSize.height / 2 - screenSize.height / 3);

        mSrcText.setBackground(new Color(219, 238, 221));
        contentPane.add(mSrcText);

        mDestText.setBackground(new Color(219, 238, 221));
        contentPane.add(mDestText);

        mSrcText.setLineWrap(true);
        contentPane.add(new JScrollPane(mSrcText));
        mDestText.setLineWrap(true);
        contentPane.add(new JScrollPane(mDestText));

        mSrcText.getDocument().addDocumentListener(mDocumentListener);
        mDestText.addMouseListener(mDestMouseAdapter);
    }

    private static String mBackSysClipboardText = "";

    /**
     * 左键双击拷贝结果,右键双击还原系统粘贴板
     */
    private static MouseAdapter mDestMouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent e) {

            if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
                if (e.getClickCount() > 1) {
                    String text = mDestText.getText();
                    if (text != null && text.length() > 0) {
                        mBackSysClipboardText = SysClipboardHelper.getSysClipboardText();
                        SysClipboardHelper.setSysClipboardText(mCopyText);
                    }
                }
            }
            // // 处理鼠标中间键
            // if ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
            // }
            if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
                if (e.getClickCount() > 1) {
                    mDestText.setText("");
                    SysClipboardHelper.setSysClipboardText(mBackSysClipboardText);
                }
            }
        }
    };

    private static DocumentListener mDocumentListener = new DocumentListener() {
        private String text = "";

        @Override
        public void removeUpdate(DocumentEvent e) {
            System.out.println("removeUpdate");
            mDestText.setText("");
        }

        /**
         * 变更时清除结果选项
         * 
         * @param e
         */
        @Override
        public void insertUpdate(DocumentEvent e) {
            System.out.println("insertUpdate");
            try {
                text = mSrcText.getText();
                if (text == null || text.length() < 1) {
                    System.err.println("没有值");
                    return;
                }
                text = text.trim();
                List<String> res = JsonFormatB.format(text);
                if (res.size() > 0) {
                    String tesS = res.get(0);
                    if (tesS != null && tesS.length() > 0) {
                        mDestText.setText(tesS);
                    }
                    mCopyText = res.get(1);
                }
            } catch (Exception ex) {
            }

        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            System.out.println("changedUpdate");
        }
    };
}
